package com.galaxy.order.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.order.domain.OrderStatusLog;
import com.galaxy.order.domain.bo.OrderStatusLogBo;
import com.galaxy.order.domain.vo.OrderStatusLogVo;
import com.galaxy.order.mapper.OrderStatusLogMapper;
import com.galaxy.order.service.OrderStatusLogService;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单状态日志信息Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class OrderStatusLogServiceImpl extends ServiceImpl<OrderStatusLogMapper, OrderStatusLog> implements OrderStatusLogService {

    private final OrderStatusLogMapper orderStatusLogMapper;
    private final Converter converter;

    @DS("slave")
    @Override
    public TableDataInfo<OrderStatusLogVo> queryPageList(OrderStatusLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderStatusLog> lqw = buildQueryWrapper(bo);
        var page = orderStatusLogMapper.selectPage(pageQuery.build(), lqw);
        List<OrderStatusLogVo> records = page.getRecords().stream()
            .map(orderStatusLog -> converter.convert(orderStatusLog, OrderStatusLogVo.class))
            .collect(Collectors.toList());
        return new TableDataInfo<>(records, page.getTotal());
    }

    @DS("slave")
    @Override
    public List<OrderStatusLogVo> queryList(OrderStatusLogBo bo) {
        LambdaQueryWrapper<OrderStatusLog> lqw = buildQueryWrapper(bo);
        return orderStatusLogMapper.selectList(lqw).stream()
            .map(orderStatusLog -> converter.convert(orderStatusLog, OrderStatusLogVo.class))
            .collect(Collectors.toList());
    }

    @DS("slave")
    @Override
    public OrderStatusLog queryById(Long id) {
        return orderStatusLogMapper.selectById(id);
    }

    @Override
    public Boolean insertByBo(OrderStatusLogBo bo) {
        OrderStatusLog add = converter.convert(bo, OrderStatusLog.class);
        return orderStatusLogMapper.insert(add) > 0;
    }

    @Override
    public Boolean updateByBo(OrderStatusLogBo bo) {
        OrderStatusLog update = converter.convert(bo, OrderStatusLog.class);
        return orderStatusLogMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO: Add validation logic here
        }
        return orderStatusLogMapper.deleteBatchIds(ids) > 0;
    }

    private LambdaQueryWrapper<OrderStatusLog> buildQueryWrapper(OrderStatusLogBo bo) {
        LambdaQueryWrapper<OrderStatusLog> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getOrderNo() != null, OrderStatusLog::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderId() != null, OrderStatusLog::getOrderId, bo.getOrderId());
        lqw.eq(bo.getStatus() != null, OrderStatusLog::getStatus, bo.getStatus());
        return lqw;
    }
}
